package com.carltechnology.mahjonggamecaculation.views.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.carltechnology.mahjonggamecaculation.R;
import com.carltechnology.mahjonggamecaculation.base.MyApplication;
import com.carltechnology.mahjonggamecaculation.mode.IGameData;
import com.carltechnology.mahjonggamecaculation.mode.impl.GameRuntimeDataMode;
import com.carltechnology.mahjonggamecaculation.presenter.IMahjongPerGameInputPresenter;
import com.carltechnology.mahjonggamecaculation.presenter.impl.MahjongPerGameInputPresenter;
import com.carltechnology.mahjonggamecaculation.utils.views.TextEditView;
import com.carltechnology.mahjonggamecaculation.views.IMahjongGamePerInputView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by WZTCM on 2016/6/30.
 */
public class MahjongGamePerInputActivity extends AppCompatActivity implements IMahjongGamePerInputView {


    public enum PERGAME_MSG{
        PRICE_NOTHING("请正确输入每人的张数"),
        COST_NOTHING("请输入台费"),
        BALANCE_UNRIHGT("平衡因子不正确，请检查输入"),
        UPDATE_BALANCE("获得新平衡因子");
        private String msg;
        PERGAME_MSG(String msg) {
            this.msg = msg;
        }
        @Override
        public String toString() {
            return msg;
        }
    }

    @InjectView(R.id.mahjonggame_perinput_person1)
    TextEditView person1View;
    @InjectView(R.id.mahjonggame_perinput_person2)
    TextEditView person2View;
    @InjectView(R.id.mahjonggame_perinput_person3)
    TextEditView person3View;
    @InjectView(R.id.mahjonggame_perinput_person4)
    TextEditView person4View;
    @InjectView(R.id.mahjonggame_perinput_cost)
    TextEditView costView;
    @InjectView(R.id.mahjonggame_perinput_balance)
    TextView balanceView;
    @InjectView(R.id.mahjonggame_perinput_container)
    RelativeLayout container;
    private IMahjongPerGameInputPresenter presenter;

    private Handler handler = new Handler();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mahjonggame_perinput);
        ButterKnife.inject(this);

        IGameData mode = MyApplication.getPool().getGameMode();
        assert mode!=null;
        String [] names = mode.getValues();
        person1View.setEditTextTag(names[0]);
        person2View.setEditTextTag(names[1]);
        person3View.setEditTextTag(names[2]);
        person4View.setEditTextTag(names[3]);

        Intent intent = getIntent();
        int pos = -1;
        GameRuntimeDataMode runningMode = intent.getParcelableExtra(MahjongRunningGameActivity.PARCEL_NAME);
        if (runningMode!=null){
            int [] content = runningMode.getGameCount();
            person1View.setEditContent(String.valueOf(content[0]));
            person2View.setEditContent(String.valueOf(content[1]));
            person3View.setEditContent(String.valueOf(content[2]));
            person4View.setEditContent(String.valueOf(content[3]));
            costView.setEditContent(String.valueOf(runningMode.getCost()));
            pos = intent.getIntExtra(MahjongRunningGameActivity.POS_NAME,-1);
        }
        presenter = MahjongPerGameInputPresenter.getInstance(this,1,pos);

    }

    @Override
    public String getPerson1Content() {
        return person1View.getEditTextContent();
    }

    @Override
    public String getPerson2Content() {
        return person2View.getEditTextContent();
    }

    @Override
    public String getPerson3Content() {
        return person3View.getEditTextContent();
    }

    @Override
    public String getPerson4Content() {
        return person4View.getEditTextContent();
    }

    @Override
    public String getCostContent() {
        return costView.getEditTextContent();
    }

    @Override
    public String[] getContents() {
        String [] contents = new String[5];
        contents[0] = person1View.getEditTextContent();
        contents[1] = person2View.getEditTextContent();
        contents[2] = person3View.getEditTextContent();
        contents[3] = person4View.getEditTextContent();
        contents[4] = costView.getEditTextContent();
        return contents;
    }

    @Override
    public Handler getHandler() {
        return handler;
    }

    @Override
    public RelativeLayout getContainer() {
        return container;
    }

    @Override
    public TextView getBalanceView() {
        return balanceView;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @OnClick(R.id.mahjonggame_perinput_yes)
    public void onYes(View view){
        presenter.examineAndStoreMahjongGameInfo();
    }
    @OnClick(R.id.mahjonggame_perinput_no)
    public void onNo(View view){
        finish();
       // GameDetailDataMode mode = GameDetailDataMode._of()
    }
}
